package com.seecen.service.impl;

import com.seecen.pojo.dto.FeedbackDTO;
import com.seecen.service.EmailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @program: SharkBookkeeping-end
 * @description:
 * @author: XXW
 * @create: 2025-10-17 21:29
 * @version: 1.0
 **/
@Service
@Slf4j
public class EmailServiceImpl implements EmailService {

    @Autowired
    private JavaMailSender mailSender;

    @Value("${spring.mail.username}")
    private String fromEmail;

    /**
     * 收件人邮箱（改成你自己的邮箱）
     * TODO: 重要！请修改为你的接收邮箱
     */
    private static final String TO_EMAIL = "3246073404@qq.com";

    @Override
    public boolean sendFeedbackEmail(FeedbackDTO feedbackDTO) {
        try {
            SimpleMailMessage message = new SimpleMailMessage();

            // 发件人
            message.setFrom(fromEmail);

            // 收件人（改成你自己的邮箱）
            message.setTo(TO_EMAIL);

            // 邮件主题
            String subject = String.format("【鲨鱼记账】用户反馈 - %s", feedbackDTO.getFeedbackType());
            message.setSubject(subject);

            // 邮件内容
            String text = buildEmailContent(feedbackDTO);
            message.setText(text);

            // 发送邮件
            mailSender.send(message);

            log.info("反馈邮件发送成功，用户：{}，类型：{}", feedbackDTO.getUserName(), feedbackDTO.getFeedbackType());
            return true;

        } catch (Exception e) {
            log.error("反馈邮件发送失败：{}", e.getMessage(), e);
            return false;
        }
    }

    /**
     * 构建邮件内容
     */
    private String buildEmailContent(FeedbackDTO feedbackDTO) {
        String currentTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

        StringBuilder content = new StringBuilder();
        content.append("═══════════════════════════════════\n");
        content.append("           鲨鱼记账 - 用户反馈\n");
        content.append("═══════════════════════════════════\n\n");

        content.append("【反馈信息】\n");
        content.append("反馈类型：").append(feedbackDTO.getFeedbackType()).append("\n");
        content.append("反馈用户：").append(feedbackDTO.getUserName()).append("\n");

        if (feedbackDTO.getContact() != null && !feedbackDTO.getContact().isEmpty()) {
            content.append("联系方式：").append(feedbackDTO.getContact()).append("\n");
        }

        content.append("反馈时间：").append(currentTime).append("\n\n");

        content.append("【反馈内容】\n");
        content.append(feedbackDTO.getContent()).append("\n\n");

        content.append("═══════════════════════════════════\n");
        content.append("请及时处理用户反馈，提升用户体验！\n");
        content.append("═══════════════════════════════════\n");

        return content.toString();
    }
}